class Game < ActiveRecord::Base
	has_many :players, :dependent => :destroy
	has_many :bids, :dependent => :destroy
	has_many :tricks, :dependent => :destroy
	belongs_to  :current_trick, :class_name => 'Trick'
	accepts_nested_attributes_for :players

	def north
		self.players.select{|p| p.position == 1}[0]
	end
	def south
		self.players.select{|p| p.position == 2}[0]
	end
	def east
		self.players.select{|p| p.position == 3}[0]
	end
	def west
		self.players.select{|p| p.position == 4}[0]
	end
 	def deal
		if self.players.size == 4
			destroy_old_cards
			n = self.north
			e = self.east
			w = self.west
			s = self.south
			deck = []
			(2..14).each do |a|
				deck << {:value=>a, :suit=>'c'}
				deck << {:value=>a, :suit=>'d'}
				deck << {:value=>a, :suit=>'h'}
				deck << {:value=>a, :suit=>'s'}
			end
			deck = deck.sort_by{rand}
			13.times do |b|
				n.add_card deck[4 * b]
				e.add_card deck[4 * b + 1]
				w.add_card deck[4 * b + 2]
				s.add_card deck[4 * b + 3]
			end
			find_dealer
		end
	end
	def destroy_old_cards
		self.north.cards.destroy_all
		self.east.cards.destroy_all
		self.west.cards.destroy_all
		self.south.cards.destroy_all
	end
	def find_dealer
		self.update_attributes(:active_player_id => self.players.sort_by{rand}[0].id)
	end
	def next_turn player_id
		@north = self.north.id
		@east = self.east.id
		@south = self.south.id
		@west = self.west.id
		case player_id
			when @north
				return @east
			when @east
				return @south
			when @south
				return @west
			when @west
				return @north
		end
	end
	def find_declarer(player_id, suit)
		player = Player.find player_id
		player_suit_bids = player.bids.select{|b| b.suit == suit}
		first_id = player_suit_bids[0].id
		partner_suit_bids = player.partner.bids.select{|b| b.suit == suit && b.id < first_id}
		if !partner_suit_bids.empty?
			return player.partner.id
		else
			return player.id
		end
	end
	def bid( player_id, value, suit)
		@passes = self.passes || 0
		@contract_final = self.contract_final
		@contract_value = self.contract_value
		@contract_suit  = self.contract_suit
		@contract_player = self.contract_player
		@dr = self.contract_double_or_redouble
		@active_player = self.next_turn(self.active_player_id)
		if value == 10 || value == 11
			if @dr.nil? || @dr < value
				@dr = value
			else
				return false
			end
		elsif value == 0 && suit == 'p'
			@passes += 1
			if @passes >= 3
				@contract_final = true
				@passes = 0
				@contract_player = self.find_declarer(@contract_player, @contract_suit)
				@active_player = next_turn @contract_player
			end
		elsif @contract_value.nil? || @contract_suit.nil?
			@contract_value = value
			@contract_suit = suit
			@contract_player = player_id
			@passes = 0
			@dr = nil
		elsif value < @contract_value || value == @contract_value && suit < @contract_suit
			return false
		else 
 			@contract_value = value
			@contract_suit = suit
			@contract_player = player_id
			@passes = 0
			@dr = nil
		end
		Bid.create :game_id => self.id, :player_id => player_id, :value => value, :suit => suit
		self.update_attributes(:contract_value => @contract_value, :contract_suit => @contract_suit, 
							   :contract_player => @contract_player, :contract_final => @contract_final,
							   :passes => @passes, :active_player_id => @active_player, :contract_double_or_redouble => @dr)
	end
	def play_card(player_id, card_id)
		if self.current_trick.nil?
			@trick = Trick.create :game_id => self.id, :player_0_id => player_id, :card_0_id => card_id, :cards_played => 1
			self.current_trick = @trick
			self.active_player_id = next_turn player_id
			self.save
		else
			@trick = Trick.find current_trick_id
			case @trick.cards_played
				when 1:	
					Trick.update_attributes(:player_1_id => player_id, :card_1_id => card_id, :cards_played => 2)
					Trick.save
				when 2:
					Trick.update_attributes(:player_2_id => player_id, :card_2_id => card_id, :cards_played => 3)
					Trick.save
				when 3:
					Trick.update_attributes(:player_3_id => player_id, :card_3_id => card_id, :cards_played => 4)
					Trick.save
			end
		end
	end
end
